<template>
  <div>
    <el-dialog ref="dislogRef" :visible.sync="dialogVisible" lock-scroll width="65%" height="50%"
      :fullscreen="fullScreen">
      <div slot="title" class="header_title">
        <span class="title_span">选择商品</span>
        <div class="title_search">
          <div>
            <el-input @change="onQuery" v-model="queryData.queryParam" placeholder="请输入商品名称\条码\编码\规格\拼音码"></el-input>
            <el-button type="primary" @click="onQuery">搜索</el-button>
          </div>
          <div>
            <el-image :src="require('@/assets/fullscreen.png')" style="width: 23px; height: 23px"
              @click="onShowColumn()"></el-image>
          </div>
        </div>
      </div>
      <div class="content_div">
        <div class="info_table">
          <div class="div_tree">
            <div class="tree_div1">
              <p>商品类别</p>
            </div>
            <div class="tree_input">
              <p>
                <input type="text" placeholder="查找" v-model="GoodsTypeName" />
                <span>
                  <i class="el-icon-search"></i>
                </span>
              </p>
            </div>
            <div class="tree_structure">
              <el-tree :data="treeData" node-key="label" :props="defaultProps" highlight-current default-expand-all
                :expand-on-click-node="false" @node-click="handleNodeClick" ref="tree"
                :filter-node-method="filterNode"></el-tree>
            </div>
          </div>
          <div class="div_table">
            <div class="table_content">
              <el-table :data="tableData.records" height="100%" @selection-change="handleSelectionChange"
                @row-dblclick="onDblclick">
                <el-table-column type="selection" width="50"> </el-table-column>
                <el-table-column :resizable="false" label="序号" type="index" width="50"></el-table-column>
                <template v-for="(item, i) in newColumnData">
                  <el-table-column :show-overflow-tooltip="true" v-if="item.show" :key="i" :prop="item.props"
                    :label="item.morenName" min-width="150px"></el-table-column>
                </template>
              </el-table>
            </div>
            <div class="table_pagination">
              <p class="total_div">共{{ tableData.total }}条</p>
              <div class="pagination_div">
                <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                  :current-page="currentPage" :page-sizes="[20, 50, 100, 200]" :page-size="pagesize" :page-count="5"
                  layout="jumper, prev, pager,next, sizes" :total="tableData.total"></el-pagination>
              </div>
            </div>
          </div>
        </div>
        <div>
        </div>
        <div class="info_button">
          <el-button @click="onAddRow">新增</el-button>
          <div style="display: flex;">
            <el-button type="primary" @click="onSelectRow">选择</el-button>
            <el-button @click="onCancelRow">取消</el-button>
          </div>
        </div>
      </div>

    </el-dialog>
    <!-- 新增商品弹框 -->
    <DialogCommodityAdd ref="commodityAddRef" @dialogVisibleClose="onCloseAddCommodity"
      @onAddSubmit="onAddCommodity"></DialogCommodityAdd>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getAction } from '@/api/manage'
import DialogCommodityAdd from '@/views/components/dialogCommodityAdd'
import { getSystemParamsByName, sysConfig } from '@/utils/index'
export default {
  name: 'dialogCommodityMultiple',
  computed: {
    ...mapGetters(['name']),
  },
  components: { DialogCommodityAdd },
  props: {
    storehouseId: '',
  },
  created() { },
  mounted() {
    this.getData()
  },
  watch: {
    dialogVisible(val) {
      if (val == true) {
        this.getData()
      } else {
        this.queryData.queryParam = ''
      }
    },
    GoodsTypeName(val) {
      this.$refs.tree.filter(val)
    },
  },
  data() {
    return {
      GoodsTypeName: '',
      queryData: {
        goodsName: '',
        xghGoodsTypeId: '',
      },
      pagesize: 20, //每页的数据条数
      currentPage: 1, //默认开始页面
      tableData: {},
      newColumnData: [
        {
          morenName: '商品编码',
          props: 'goodsNumber',
          show: true,
        },
        {
          morenName: '条形码',
          props: 'barCode',
          show: true,
        },
        {
          morenName: '商品名称',
          props: 'goodsName',
          show: true,
        },
        {
          morenName: '基本单位',
          props: 'baseUnit_dictText',
          show: true,
        },
        {
          morenName: '规格',
          props: 'specs',
          show: true,
        },
        {
          morenName: '库存数量',
          props: 'number',
        },
        {
          morenName: '可用数量',
          props: 'useNumber',
        },
        {
          morenName: '辅助单位',
          props: 'helpUnit',
          show: true,
        },
        {
          morenName: '辅助单位规格',
          props: 'specsall',
          show: true,
        },
        {
          morenName: '商品类别',
          props: 'xghGoodsTypeName',
          show: true,
        },
        {
          morenName: '零售价',
          props: 'retailPrice',
          show: true,
        },
        {
          morenName: '预设售价一',
          props: 'presetPriceOne',
          show: true,
        },
        {
          morenName: '预设售价二',
          props: 'presetPriceTwo',
          show: true,
        },
        {
          morenName: '预设进价',
          props: 'presetBidPrice',
          show: true,
        },
        {
          morenName: '商品性质',
          props: 'goodsNatureName',
          show: true,
        },
        {
          morenName: '预设折扣一',
          props: 'discountRatioOne',
          show: true,
        },
        {
          morenName: '预设折扣二',
          props: 'discountRatioTwo',
          show: true,
        },
        {
          morenName: '拼音码',
          props: 'pinyinCode',
          show: true,
        },
        {
          morenName: '备注',
          props: 'goodsRemark',
          show: true,
        },
      ],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      dialogVisible: false, // 新增弹框
      showGoodsStoreNumber: false,
      showGoodsUsableNumber: false,
      fullScreen: false,
      // 传递给新增商品的初始化值
      newData: {
        id: '',
        goodsName: '', //商品名称
        pinyinCode: '',
        goodsNumber: '', //商品编码
        productionAddress: '', //产地
        goodsNatureId: '1', //商品性质id 1：服务、2.劳务 3.商品（无成本）
        goodsNatureName: '',
        xghGoodsTypeId: '', //商品类别
        xghGoodsTypeName: '',
        costCalculation: '1', //成本核算
        barCode: '', //条形码
        specs: '', //规格
        defaultUnit: '', //默认单位
        baseUnit: '', //基本单位
        multiPriceStatus: false, //多单位状态 0:开启 1:关闭
        priceStatus: true, //价格管理状态 0:开启 1:关闭
        stockWarningStatus: true, //库存预警状态 0:开启 1:关闭
        multiStockWarningStatus: false, //分库存预警状态 0:开启 1:关闭
        stockStatus: false, //期初库存状态 0:开启 1:关闭
        multiStockStatus: false, //分期初库存状态 0:开启 1:关闭
        goodsRemark: '', //备注
        goodsManyNorms: [
          //多单位及价格表数据
          {
            fakeId: '0',
            isDefault: '1', //是否是基本单位
            unitType: '基本单位', //单位类别 1.基本单位2.辅助单位
            unit: '', //单位
            scale: 1, //换算比例
            retailPrice: '', //零售价
            barCode: '', //条形码
            specs: '', //规格
            vipPric: '', //会员价
            presetPriceOne: '', //预设售价1
            presetPriceTwo: '', //预设售价2
            presetBidPrice: '', //预设进价
            discountRatioOne: '', //预设折扣1
            discountRatioTwo: '', //预设折扣2
            xhState: true,
          },
        ],
        goodsWarnings: [
          //仓库预警数据
          {
            fakeId: '0',
            xghStorehouseId: '', //仓库id
            xghStorehouseName: '总数量预警',
            miniStock: 0, //最低库存
            highStock: 0, //最高库存
            xhState: true,
          },
        ],
        initialInventoryInfos: [],
        multipleSelection: [], // 存储多选的数据
      },
    }
  },
  methods: {
    // 获取数据
    getData() {
      this.handleDataColumn()
      this.getTreeData()
      this.getTableData()
    },
    // 处理数据列,根据系统配置，决定选择商品时是否显示可用数量、库存数量
    handleDataColumn() {
      let config = sysConfig()
      this.newColumnData.forEach((item) => {
        if (item.props == 'number') {
          // 库存数量
          item.show = config.show_shorehouse_num
        } else if (item.props == 'useNumber') {
          // 可用数量
          item.show = config.show_available_num
        } else {
          item.show = true
        }
      })
    },
    // 获取类别数据
    getTreeData() {
      let data = {
        GoodsTypeName: this.GoodsTypeName,
      }
      getAction('/xghGoodsType/xghGoodsType/typeList', {}).then((res) => {
        if (res.code === 200) {
          let list = res.result
          let result = this.getTreeDataByList(list, '0', '')
          this.treeData = [
            {
              id: '',
              label: '全部',
              children: result,
            },
          ]
        }
      })
    },
    getTreeDataByList(list, parentId, parentName) {
      let data = []
      for (let index = 0; index < list.length; index++) {
        const element = list[index]
        if (element.pid == parentId) {
          data.push({
            ...element,
            id: element.id,
            label: element.goodsTypeName,
            parentId,
            parentName,
            children: this.getTreeDataByList(list, element.id, element.companyTypeName),
          })
        }
      }
      return data
    },
    // 获取表格数据
    getTableData(page) {
      let data = {
        ...this.queryData,
        storehouseId: this.storehouseId,
        pageSize: this.pagesize,
        pageNo: page || 1,
      }
      getAction('/xghGoods/xghGoods/list', data).then((res) => {
        if (res.code === 200) {
          this.tableData = res.result
          this.tableData.records.forEach((item) => {
            item.disabled_status = item.disabled_status == 0 ? true : false
          })
        }
      })
    },
    // 新增
    onAddRow() {
      const that = this
      this.$nextTick(() => {
        that.$refs.commodityAddRef.show(that.newData)
      })
    },
    // 获取新增商品数据
    onAddCommodity() {
      // this.getTableData()
      this.newData = this.$options.data().newData
    },
    // 关闭新增商品时清空
    onCloseAddCommodity() {
      this.getData()
      this.newData = this.$options.data().newData
    },
    // 搜索
    onQuery() {
      this.getTableData()
    },
    // 全屏显示
    onShowColumn() {
      this.fullScreen = !this.fullScreen
    },
    // 全屏显示
    toDetail() {
      this.$router.push({
        name: 'commodityInfo',
      })
    },
    // 搜索树形结构数据
    onSelectTree() {
      this.getTreeData()
    },
    // 触发树形当前节点
    handleNodeClick(data) {
      if (data.superiorId === 0) {
        this.queryData.xghGoodsTypeId = ''
      } else {
        this.queryData.xghGoodsTypeId = data.id
      }
      this.nowLabel = data.goodsTypeName
      this.getTableData()
    },
    // 触发表格多选按钮
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 表格双击
    onDblclick(val) {
      // 双击选择该商品
      this.multipleSelection = [val]
      this.onSelectRow()
    },
    // 选中后传递给父组件
    onSelectRow() {
      if (this.multipleSelection == null || this.multipleSelection.length === 0) {
        this.$elMessage.warning('请选择一条信息')
      } else {
        this.$emit('onSelectCommodityMultiple', this.multipleSelection)
        this.dialogVisible = false
      }
    },
    // 取消
    onCancelRow() {
      this.$emit('onCancelCommodity', this.multipleSelection)
      this.dialogVisible = false
    },
    // 获取每页几条数据
    handleSizeChange(val) {
      this.pagesize = val
      this.getTableData()
    },
    // 获取当前第几页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTableData(val)
    },
    // 商品类别数据筛选
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;

  .el-dialog__body {
    padding: 0;
  }

  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dialog__headerbtn {
      position: static;

      .el-icon-close {
        color: #999;
        font-size: 30px;
      }
    }
  }

  .header_title {
    width: 100%;
    display: flex;
    align-items: center;

    .title_span {
      white-space: nowrap;
      font-size: 24px;
      color: #666;
      font-weight: 600;
    }

    .title_search {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 0.468779rem 0 1.875117rem;

      >div {
        display: flex;
        align-items: center;

        .el-input {
          width: 4.725295rem;
          height: 0.562535rem;
          margin-right: 0.112507rem;

          .el-input__inner {
            height: 0.562535rem;
            border-color: #dddddd;
            box-sizing: border-box;
            padding: 0 0.150009rem;
            background: #fff;
            font-size: 12px;
            color: #666;

            &::placeholder {
              font-size: 12px;
              color: #c1c1c1;
            }
          }
        }

        .el-button {
          width: 1.12507rem;
          height: 0.562535rem;
          text-align: center;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 12px;
          border-radius: 0.093756rem;
          border: none;
          margin-right: 0.112507rem;

          span {
            color: #fff !important;
            font-size: 14px !important;
            font-weight: normal !important;
          }
        }
      }
    }

    .set_up {
      font-size: 23px;
      color: rgb(153, 153, 153);
    }
  }
}

.content_div {
  width: 100%;
  height: 60vh;
  background: #fff;
  box-sizing: border-box;
  padding: 0.187512rem 0.281268rem;

  .info_table {
    width: 100%;
    height: calc(100% - 0.750047rem);
    display: flex;

    .div_tree {
      width: 2.812676rem;
      height: calc(100% - 1.12507rem);
      box-sizing: border-box;
      padding: 0.131258rem 0;
      border: 0.018751rem solid #e3e3e3;
      transition: all 0.3s;

      .tree_div1 {
        width: 100%;
        height: 0.337521rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;
        display: flex;
        align-items: center;
        justify-content: space-between;

        p {
          color: #666666;
          font-size: 12px;
        }
      }

      .tree_input {
        width: 100%;
        height: 0.562535rem;
        margin-top: 0.131258rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;

        p {
          width: 100%;
          height: 0.562535rem;
          margin: 0;
          box-sizing: border-box;
          border: 1px solid #dddddd;
          display: flex;
          align-items: center;
          position: relative;

          input {
            width: 100%;
            box-sizing: border-box;
            padding: 0 0.600038rem 0 0.093756rem;
            height: 0.525033rem;
            border: none;
            font-size: 12px;

            &:focus {
              outline: none;
            }
          }

          span {
            position: absolute;
            right: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 0.562535rem;
            height: 0.525033rem;
            background: #fff;

            .el-icon-search {
              font-size: 16px;
              color: rgb(211, 211, 211);
            }
          }
        }
      }

      .tree_structure {
        width: 100%;
        height: calc(100% - 0.337521rem - 0.131258rem - 0.131258rem - 0.562535rem);
        box-sizing: border-box;
        padding: 0 0.168761rem;
        margin-top: 0.131258rem;
        overflow: auto;

        ::v-deep .el-tree {
          width: 100%;

          .el-tree-node__children {
            .el-tree-node__label {
              color: #666;
              font-weight: normal;
            }
          }

          .el-tree-node__label {
            font-size: 12px;
            font-weight: 600;
          }

          .is-current {
            .el-tree-node__content {
              background-color: #e6f7ff;
            }
          }

          .el-tree-node__content>.el-tree-node__expand-icon {
            padding-left: 0;
          }
        }
      }
    }

    .div_table {
      width: calc(100% - 3.000188rem);
      height: 100%;
      margin-left: 0.187512rem;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      transition: all 0.3s;

      .table_content {
        height: calc(100% - 1.12507rem);

        ::v-deep .el-table {
          border: 1px solid #e3e3e3;
          box-sizing: border-box;

          th,
          td {
            padding: 0;
            color: #666;
            box-sizing: border-box;
            border-right: 1px solid #e3e3e3;

            .cell {
              height: 0.562535rem;
              display: flex;
              align-items: center;
              font-size: 12px;
            }

            &:first-child {
              .cell {
                justify-content: center;
              }
            }

            &:last-child {
              border-right: none;
            }
          }

          th {
            .cell {
              background: #ededed;
              justify-content: center;
            }
          }

          .el-table__body tr:hover>td {
            background: #e3f6ff;
          }

          .el-table__footer-wrapper {
            td {
              background: #f4f4f4;
              border: none;
            }
          }

          //滚动条的宽度
          ::-webkit-scrollbar {
            width: 10px;
            height: 10px;
            background-color: #e5e5e5;
          }

          //滚动条的滑块
          ::-webkit-scrollbar-thumb {
            background-color: #c9c9c9;
          }
        }
      }

      .table_pagination {
        width: 100%;
        height: 1.12507rem;
        box-sizing: border-box;
        padding: 0.168761rem 0;
        display: flex;

        .total_div {
          line-height: 0.787549rem;
          font-size: 12px;
          color: #666;
          margin: 0;
          white-space: nowrap;
        }

        .pagination_div {
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: flex-end;

          ::v-deep .el-pagination {
            padding: 0;
            height: 0.525033rem;

            .el-pagination__sizes {
              margin: 0;

              .el-input {
                margin-right: 0;
              }
            }
          }
        }
      }
    }
  }

  .info_button {
    width: 100%;
    height: 0.750047rem;
    display: flex;
    justify-content: space-between;

    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.093756rem;
    }
  }
}

::v-deep .select_row {
  background-color: #e3f6ff !important;
}
</style>